package repository

import (
	"context"
	"github.com/ecodeclub/ekit/slice"
	"jk-time/webook/search/dao"
	"jk-time/webook/search/domain"
)

type ArticleRepository interface {
	InputArticle(ctx context.Context, msg domain.Article) error
	SearchArticle(ctx context.Context, uid int64, keywords []string) ([]domain.Article, error)
}
type articleRepository struct {
	aDao dao.ArticleElasticDAO
	tDao dao.TagElasticDAO
	cDao dao.CollectElasticDAO
	lDao dao.LikeElasticDAO
}

func (a *articleRepository) InputArticle(ctx context.Context, msg domain.Article) error {
	return a.aDao.InputArticle(ctx, dao.Article{
		Id:      msg.Id,
		Title:   msg.Title,
		Status:  msg.Status,
		Content: msg.Content,
	})
}
func (a *articleRepository) SearchArticle(ctx context.Context, uid int64,
	keywords []string) ([]domain.Article, error) {
	//出自这里
	artIDs1, err := a.tDao.SearchTag(ctx, uid, "article", keywords)
	if err != nil {
		return nil, err
	}
	artIDs2, err := a.cDao.Search(ctx, uid, "article")
	if err != nil {
		return nil, err
	}
	artIDs3, err := a.lDao.Search(ctx, uid, "article")
	if err != nil {
		return nil, err
	}

	arts, err := a.aDao.Search(ctx, dao.ArticleReq{
		artIDs1, artIDs2, artIDs3,
	}, keywords)
	if err != nil {
		return nil, err
	}
	return slice.Map(arts, func(idx int, src dao.Article) domain.Article {
		return domain.Article{
			Id:      src.Id,
			Title:   src.Title,
			Status:  src.Status,
			Content: src.Content,
			Tags:    src.Tags,
		}
	}), nil
}
